/**
 * @author KujoMiva
 * @name CheckText.ts
 * @desc 选择祝福语界面
 * @time 2020年02月09日 01:26:12 星期天
 */
class CheckText extends egret.DisplayObjectContainer {
  private animtionSpeed = 200
  public constructor() {
    super()
    this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
  }

  private init() {
    this.createBg()
    this.createBtnGroup()
    // this.createBtnReset()
    this.createBtnSubmit()
    this.createHeart()
  }

  private createBg() {
    const bg = Utils.createBitmapByName('bg_text_png')
    Utils.createScale(bg, this)
    bg.scaleX = bg.scaleX - .03
    bg.scaleY = bg.scaleY - .03
    Utils.anchorCenter(bg)
    bg.x = this.stage.stageWidth / 2
    bg.y = this.stage.stageHeight / 2
  }

  private page = true;
  private container: egret.Sprite;
  private createBtnGroup() {
    this.container = new egret.Sprite()
    this.addChild(this.container)

    for (let i = 1; i <= 8; i++) {
      this.createBtnText(i, this.page ? 0 : 8, this.container)
    }

    this.once('reget', () => {
      egret.Tween.get(this).wait(this.animtionSpeed).call(() => {
        this.removeChild(this.container)
        this.createBtnGroup()
      })
    }, this)

  }
  // 创建祝福语按钮
  private createBtnText(num, page, container) {
    const btn = Utils.createBitmapByName(`btn_${num + page}_png`)
    Utils.createScale(btn, container)
    Utils.anchorCenter(btn)
    const { scaleX, scaleY } = btn
    const btnH = btn.height / 2
    const paddingTop = 30
    const baseTop = 190 + btn.height / 4
    const pathObj = {
      1: baseTop,
      3: baseTop + (btnH + paddingTop),
      5: baseTop + (btnH + paddingTop) * 2,
      7: baseTop + (btnH + paddingTop) * 3,
    }

    btn.x = num % 2 === 0 ? 65 + (btn.width / 4) : 340 + (btn.width / 4)
    btn.y = num % 2 === 0 ? pathObj[num - 1] : pathObj[num]

    btn.scaleX = 0
    btn.scaleY = 0
    egret.Tween.get(btn).to({ scaleX: scaleX, scaleY: scaleY }, this.animtionSpeed).call(() => {
      this.dispatchEvent(new egret.Event('reget_over'))
    })

    btn.touchEnabled = true
    btn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      this.dispatchEvent(new egret.Event('clearCheck'))
      btn.texture = RES.getRes(`btn_${num + page}_act_png`);
      ParamsPool.CHECK_NUM = num + page
    }, this)

    this.addEventListener('clearCheck', () => {
      btn.texture = RES.getRes(`btn_${num + page}_png`);
    }, this)

    // 就是因为这玩意, 每调一次就会发送出去八次事件, 按五次已经会调用40次 reget_zero了
    this.addEventListener('reget', () => {
      egret.Tween.get(btn).to({ scaleX: 0, scaleY: 0 }, this.animtionSpeed).call(() => {
        //     this.dispatchEvent(new egret.Event('reget_zero'))
      })
    }, this)

  }
  // 创建更换按钮
  private btnResetSwitch = true
  private createBtnReset() {
    const btn = Utils.createBitmapByName('btn_reget_png')
    Utils.createScale(btn, this)
    Utils.anchorCenter(btn)
    btn.x = this.stage.stageWidth / 2
    btn.y = 940
    btn.touchEnabled = true
    btn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      if (!this.btnResetSwitch) {
        console.log('wait btnResetSwitch !')
        return
      }
      this.btnResetSwitch = false
      this.page = !this.page
      this.dispatchEvent(new egret.Event('reget'))
    }, this);

    this.addEventListener('reget_over', () => {
      this.btnResetSwitch = true
    }, this)
  }
  // 创建提交按钮
  private createBtnSubmit() {
    const btn = Utils.createBitmapByName('btn_submit_png')
    Utils.createScale(btn, this)
    Utils.anchorCenter(btn)
    btn.x = this.stage.stageWidth / 2
    this.addChild(btn)
    btn.y = 1000
    btn.touchEnabled = true
    btn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
      if (!ParamsPool.CHECK_NUM) {
        alert('请选择祝福语')
        return
      }
      // this.stage.dispatchEvent(new egret.Event('gotoCYS'))
      this.dispatchEvent(new egret.Event('next'))
      // this.parent.removeChild(this)
      console.log('点击了提交按钮, 选择了: ', ParamsPool.CHECK_NUM)
    }, this);
  }

  private createHeart() {
    const heart = Utils.createBitmapByName('item_heart_png')
    Utils.createScale(heart, this, .05)
    Utils.anchorCenter(heart)
    Utils.dragGetPatn(heart, this)
    heart.x = 205
    heart.y = 1000
    heart.alpha = 0
    this.addEventListener('next', () => {
      egret.Tween.get(heart)
        .to({ x: 320, y: 170, alpha: 1 }, 800)
        .call(() => {
          this.stage.dispatchEvent(new egret.Event('gotoCYS'))
          console.log('gotoCYS')
          egret.Tween.get(this).to({ alpha: 0 }, 1000).call(() => this.parent.removeChild(this))
        })
    }, this)
  }
}